<?php

namespace think\auth\model;

use think\Model;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

/**
 * 权限.
 */
class PermissionModel extends Model
{
    public function __construct($data = [])
    {
        $this->name = config('auth.permission.table');
        parent::__construct($data);
    }

    /**
     * 通过名称查找规则
     * @param string $name
     * @return PermissionModel|null
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public static function FindByName(string $name): PermissionModel|null
    {
        return self::where(['name' => $name])->find();
    }

    /**
     * 通过ID查找规则
     * @param int|string $permissionId
     * @return PermissionModel|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function FindById(int|string $permissionId): PermissionModel|null
    {
        return self::where([config('auth.permission.primaryKey') => $permissionId])->find();
    }
}
